package com.libbase.fragment

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import com.base.load.DefaultLoad
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chan.net.Result
import com.libbase.R
import com.libbase.adapter.NyqfNewAdapter
import com.libbase.bean.NyqfBean
import com.base.kotlin.mvp.BaseMvpKFragment
import com.libbase.kotlin.presenter.NewContract
import com.libbase.kotlin.presenter.NewPresenter
import com.uitls.utils.RecyclerViewUtil
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.fragment_nyqf.*

class NewMvpKFragment : BaseMvpKFragment<NewPresenter>(), NewContract.View {
    private var mPosition: Int = 0
    private var mLoadView: DefaultLoad? = null
    private var mFragmentCallBack: NewFragment.FragmentCallBack? = null
    private var code: String? = null
    private var mAdapter: NyqfNewAdapter? = null
    private var mPositionForVideo: Int? = null

    companion object {
        fun newInstance(tag: String, code: String): NewMvpKFragment {
            val mainFragment = NewMvpKFragment()
            val bundle = Bundle()
            bundle.putString("name", tag)
            bundle.putString("code", code)
            mainFragment.arguments = bundle
            return mainFragment
        }
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity is NewFragment.FragmentCallBack) {
            mFragmentCallBack = activity
        }
    }

    override fun onDetach() {
        super.onDetach()
        mFragmentCallBack = null
    }

    override fun bindViewToPresenter(): NewPresenter? {
        val presenter = NewPresenter()
        presenter.attachView(this)
        return presenter
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_nyqf
    }

    override fun initView(savedInstanceState: Bundle?) {
        mLoadView = DefaultLoad(mContext)

        code = arguments?.getString("code")
        RecyclerViewUtil.setLinearLayoutManagerWithDivder(mContext, rcv_main)
        mAdapter = NyqfNewAdapter()
        rcv_main.adapter = mAdapter
        val itemDecoration: RecyclerView.ItemDecoration? = HorizontalDividerItemDecoration.Builder(mContext)
                .size(2)
                .color(Color.parseColor("#99b2b2b2"))
                .marginResId(R.dimen.dp15, R.dimen.dp15)
                .build()
        itemDecoration?.let {
            rcv_main.addItemDecoration(it)
        }

        code?.let {
            mPresenter?.getNews(it)
        }

        initLister()
    }

    private fun initLister() {
        mAdapter?.setOnItemChildClickListener { adapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, position: Int ->
            var newBean: NyqfBean.NewBean? = adapter.data[position] as NyqfBean.NewBean
            newBean?.let {
                when (view.id) {
                    R.id.fl_item_nyqf_video -> if (newBean.itemType == NyqfNewAdapter.ITEM_VIDEO) {
                        if (mFragmentCallBack == null) {
                            return@setOnItemChildClickListener
                        }
                        val videoView = mFragmentCallBack?.getVideoView()
                        val frameLayout = view as FrameLayout
                        refreshViewIn(frameLayout)
                        mPosition = position
                        videoView?.setUp(newBean.poster, null)
                        videoView?.start(0)
                    }
                    R.id.ll_item_nyqf -> if (mFragmentCallBack != null) {
                        mFragmentCallBack?.removePlayerView()
                        //AdvertiseActivity.startActivity(mContext, "http://yxb.southtv.cn:9180/api/nyqf/" + newBean.getId() + ".html", AdvertiseActivity.TYPE_NYQF);
                    }
                }
            }
        }
        rcv_main.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (mFragmentCallBack != null && mFragmentCallBack?.hasVideoViewParent() as Boolean) {
                        val wrap: LinearLayoutManager? = recyclerView.layoutManager as LinearLayoutManager
                        if (wrap != null) {
                            val lastPosition = wrap.findLastVisibleItemPosition()
                            val firstPosition = wrap.findFirstVisibleItemPosition()
                            mPositionForVideo?.let {
                                if (it < firstPosition || it > lastPosition) {
                                    mFragmentCallBack?.removePlayerView()
                                }
                            }
                        }
                    }
                }
            }
        })
    }

    override fun release() {
    }

    override fun onNewsSuccess(result: Result<NyqfBean>) {
        mAdapter?.setNewData(result.data.news)
    }

    override fun showLoading() {
        mLoadView?.load(fl_content)
    }

    override fun hideLoading() {
        mLoadView?.removeView()
    }

    override fun onError(e: Throwable) {

    }

    private fun refreshViewIn(frameLayout: FrameLayout?) {
        if (mFragmentCallBack == null) {
            return
        }
        mFragmentCallBack?.removePlayerView()
        val videoView = mFragmentCallBack?.videoView
        mPosition = -1
        if (frameLayout != null) {
            val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER)
            frameLayout.addView(videoView, frameLayout.childCount, params)
        }
    }
}